package com.whl215.kit.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.SparseArray
import android.view.View
import android.widget.*
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.annotation.IntDef
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

/**
 * author： happiness
 * date： 2021/2/22
 * description：ViewHolder 的作用就是 保存 持有view 引用 当抽象出一个BaseVH 之后 不需要创建其他的更具体的VH
 * 更具体的VN 不过是 布局不同 findViewById的view不同  我们抽象的BaseVH 一个就搞定了
 * 是用一个原生的 RecyclerView.ViewHolder() 传入不同itemView 也可以达到展示多种view 的效果 .
 * 不过原生VH 不能进行数据绑定而已
 *
 * 如果使用ViewBinding  更简单  其他setXXX() 方法全都不用提供 通过viewBinding 直接使用view
 */
@Suppress("UNCHECKED_CAST")
class GrapeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    private val mViews: SparseArray<View> = SparseArray()
    private val context: Context = itemView.context.applicationContext
    private var mOnClickListener: View.OnClickListener? = null
    private  var mViewBinding: ViewBinding ?=null

    constructor(viewBinding: ViewBinding) : this(viewBinding.root) {
        mViewBinding = viewBinding
    }
    
    fun <VB : ViewBinding> getViewBinding(): VB {
        return mViewBinding as VB
    }
    fun setOnClickListener(view:View):GrapeViewHolder{
        if (mOnClickListener !=null){
            view.setOnClickListener(mOnClickListener)
        }
        return this
    }

    /**
     * adapter 实现 OnChildClickListener接口 调用次方法
     */
    fun setOnClickListener(onClickListener: View.OnClickListener) {
        this.mOnClickListener = onClickListener
    }

    fun getContext(): Context {
        return context
    }
    
    
    
    
    fun setText(@IdRes id: Int, text: String?): GrapeViewHolder {
        val textView = getView<TextView>(id)
        textView.text = text
        return this
    }
    
    fun setText(@IdRes id: Int, text: CharSequence?): GrapeViewHolder {
        val textView = getView<TextView>(id)
        textView.text = text
        return this
    }
    
    fun setTextSize(@IdRes id: Int, textSize: Float): GrapeViewHolder {
        val textView = getView<TextView>(id)
        textView.textSize = textSize
        return this
    }
    
    fun setTextColor(@IdRes id: Int, @ColorInt color: Int): GrapeViewHolder {
        val textView = getView<TextView>(id)
        textView.setTextColor(color)
        return this
    }
    
    fun setMaxLines(@IdRes id: Int, maxLines: Int): GrapeViewHolder {
        val textView = getView<TextView>(id)
        textView.maxLines = maxLines
        return this
    }
    
    fun setProgress(@IdRes id: Int, progress: Int): GrapeViewHolder {
        val progressBar = getView<ProgressBar>(id)
        progressBar.progress = progress
        return this
    }
    
    fun setBackgroundColor(@IdRes id: Int, @ColorInt color: Int): GrapeViewHolder {
        val view = getView<View>(id)
        view.setBackgroundColor(color)
        return this
    }
    
    fun setBackground(@IdRes id: Int, background: Drawable?): GrapeViewHolder {
        val view = getView<View>(id)
        view.background = background
        return this
    }
    
    fun setBackgroundResource(@IdRes id: Int, @DrawableRes resId: Int): GrapeViewHolder {
        val view = getView<View>(id)
        view.setBackgroundResource(resId)
        return this
    }
    
    fun setTag(@IdRes id: Int, tag: Any?): GrapeViewHolder {
        val view = getView<View>(id)
        view.tag = tag
        return this
    }
    
    fun setClickable(@IdRes id: Int, clickable: Boolean): GrapeViewHolder {
        val view = getView<View>(id)
        view.isClickable = clickable
        return this
    }
    
    fun setEnabled(@IdRes id: Int, enabled: Boolean): GrapeViewHolder {
        val view = getView<View>(id)
        view.isEnabled = enabled
        return this
    }

    
    fun setChecked(@IdRes id: Int, isChecked: Boolean): GrapeViewHolder {
        val view: Checkable = getView(id)
        view.isChecked = isChecked
        return this
    }
    
    fun setOnCheckedChangeListener(
        @IdRes id: Int,
        listener: CompoundButton.OnCheckedChangeListener?
    ): GrapeViewHolder {
        val View = getView<CompoundButton>(id)
        View.setOnCheckedChangeListener(listener)
        return this
    }
    
    fun setImageResource(@IdRes id: Int, @DrawableRes imgRes: Int): GrapeViewHolder {
        val imageView = getView<ImageView>(id)
        imageView.setImageResource(imgRes)
        return this
    }
    
    fun setImageDrawable(@IdRes id: Int, drawable: Drawable?): GrapeViewHolder {
        val imageView = getView<ImageView>(id)
        imageView.setImageDrawable(drawable)
        return this
    }
    fun setVisibility(@IdRes id: Int, @Visibility visibility: Int): GrapeViewHolder {
        val view = getView<View>(id)
        view.visibility = visibility
        return this
    }
    

    
    fun setOnClickListener(@IdRes id: Int): GrapeViewHolder {
        if (mOnClickListener != null) {
            val view = getView<View>(id)
            view.setOnClickListener(mOnClickListener)
        }
        return this
    }

    
    fun setOnClickListener(
        @IdRes id: Int,
        onClickListener: View.OnClickListener?
    ): GrapeViewHolder {
        val view = getView<View>(id)
        view.setOnClickListener(onClickListener)
        return this
    }



    /**
     * 通过viewId获取控件
     *
     * @param viewId viewId
     * @return view
     */
   private fun <T> getView(viewId: Int): T {
        var view: View? = mViews.get(viewId)
        if (view == null) {
            view = itemView.findViewById(viewId)
            mViews.put(viewId, view)
        }
        return view as T
    }


    /**
     * 控制view 显示隐藏
     */
    @IntDef(View.VISIBLE, View.INVISIBLE, View.GONE)
    @kotlin.annotation.Retention(AnnotationRetention.SOURCE)
    internal annotation class Visibility
}